<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/7/22
 * Time: 13:30
 */

namespace Api\Provider\Admin;


use Api\Provider\IndexProvider;
use Api\Service\ImageScoreService;
use Api\Service\ImageService;
use Api\Service\ScoreCommentService;
use Api\Service\ScoreFavoriteService;
use Api\Service\ScoreService;
use Api\Service\SheetService;
use Api\Service\TagScoreService;
use Think\Exception;

class ScoreProvider extends IndexProvider
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 首页接口
     */
    public function index($param)
    {
        $limit = empty($param['limit']) ? 6 : $param['limit'];
        $service = new ScoreService();
        $map = array('is_delete'=>0);
        $hot = M( 'ScoreHot' )->where($map)->order('is_top desc')->group('score_id')->limit($limit)->select();
        $hotIds = array_column($hot,'score_id');
        $list['hot'] = $service->getScorePageByScoreIds($hotIds, $param);

        $free = M( 'ScoreFree' )->where($map)->order('is_top desc')->group('score_id')->limit($limit)->select();
        $freeIds = array_column($free,'score_id');
        $list['free'] = $service->getScorePageByScoreIds($freeIds, $param);

        $new = M( 'ScoreNew' )->where($map)->order('is_top desc')->group('score_id')->limit($limit)->select();
        $newIds = array_column($new,'score_id');
        $list['new'] = $service->getScorePageByScoreIds($newIds, $param);

        $this->success($list,200);
    }

    /**
     * 通过用户Uid获取用户创建的谱单
     */
    public function getScoreByUid($param)
    {
        $uid = $param['uid'];

        if (empty($uid)) {
            $this->fail('600');
        }
        $map = array(
            'creator' => $uid,
            'is_delete' => 0
        );
        //通过uid获取用户谱单
        $list = M('Score')->where($map)->field('id,name,desc,creator,is_private, author_type, comment_count')->select();
        $scoreIds = array_column($list, 'id');

        //获取谱单创建者信息及标签
        $service = new ScoreService();
        $list = $service->convertList($list);


        //获取谱单封面
        $imageService = new ImageService();
        $imagemap = $imageService->getImageUrlByScoreIds($scoreIds);
        foreach ($list as $key => $score) {
            $list[$key]['image'] = $imagemap[$score['id']];
        }

        $this->success($list,200);
    }

    /**
     * 获取用户收藏的谱单
     */
    public function getFavScoreByUid($param)
    {
        $uid = $param['uid'];
        if (empty($uid)) {
            $this->fail('600');
        }
        $map = array(
            'user_id'=>$uid,
            'is_delete' => 0
        );
        //通过uid获取用户收藏谱单id
        $list = M('ScoreFavorite')->where($map)->field('id,score_id,user_id')->select();
        $scoreIds = array_column($list, 'score_id');

        //获取谱单基本信息
        $map = array('id' => array('in', $scoreIds));
        $list = M('Score')->where($map)->field('id,name,desc,creator,is_private, author_type')->select();

        //获取谱单创建者信息及标签
        $service = new ScoreService();
        $list = $service->convertList($list);

        //获取谱单封面
        $imageService = new ImageService();
        $imagemap = $imageService->getImageUrlByScoreIds($scoreIds);
        foreach ($list as $key => $score) {
            $list[$key]['image'] = $imagemap[$score['id']];
        }
        $this->success($list,200);
    }

    /**
     * 用户新增谱单
     */
    public function addScore($param)
    {
        if (empty($param['uid']) || empty($param['name'])) {
            $this->fail(600);
        }
        $service = new ScoreService();
        try{
            $id = $service->add($param);
            $this->success('',200,'新建成功');
        } catch(\Exception $e){
            $this->fail(500,'', $e->getMessage());
        }
    }

    /**
     * 获取谱单基本信息
     */
    public function getScoreBasicInfo($param)
    {
        if (empty($param['score_id'])) {
            $this->fail(600);
        }
        //1.获取谱单信息
        $list = M('Score')->where(array('id'=>$param['score_id']))->field('id, name, desc')->select();
        if (empty($list)) {
            $this->fail(500, '', array('获取谱单信息失败'));
        }
        //2.获取图片
        $imgService = new ImageService();
        $img = $imgService->getImageUrlByScoreIds($param['score_id']);
        $list[0]['img'] = $img[$param['score_id']];
        //3.TODO::获取谱单标签

        $this->success($list);

    }
    /**
     * 用户编辑谱单
     */
    public function editScore($param)
    {
        //uid, name, desc, img_name, img_url,
        if (empty($param['uid']) || empty($param['score_id']) ) {
            $this->fail(600);
        }

        $service = new ScoreService();
        try {
            $id = $service->edit($param);
            $this->success('', 200, '操作成功');
        } catch (\Exception $e) {
            $this->fail( 500, '', array($e->getMessage()) );
        }
}

    /**
     * 用户删除谱单
     */
    public function delScore($param)
    {
        if (empty($param['score_id']) || empty($param['uid'])) {
            $this->fail(600);
        }
        $service = new ScoreService();
        $res = $service->del($param);
        if ($res === 0) {
            $this->fail(403);
        } elseif(!$res) {
            $this->fail(500);
        } else {
            $this->success('',200);
        }
    }

    /**
     * 用户收藏谱单
     */
    public function addFavScore($param)
    {
        if (empty($param['uid']) || empty($param['score_id'])) {
            $this->fail(600);
        }
        $service = new ScoreFavoriteService();
        try {
            $res = $service->addFavScore($param);
            $this->success('',200);
        } catch (\Exception $e) {
            $this->fail(500,'', $e->getMessage());
        }
    }

    /**
     * 分页获取最热专区谱单
     */
    public function getScoreHot($param)
    {
        $limit = $param['limit'];
        $map = array(
            'is_delete' => 0
        );
        $res = M( 'ScoreHot' )->where($map)->order('is_top desc')->group('score_id')->limit($limit)->select();

        $scoreIds = array_column($res,'score_id');
        $service = new ScoreService();
        $list = $service->getScorePageByScoreIds($scoreIds, $param);
        if($list) {
            $this->success( $list,200);
        } else {
            $this->fail('500');
        }
    }

    /**
     * 分页获取免费专区谱单
     */
    public function getScoreFree($param)
    {
        $limit = $param['limit'];
        $map = array(
            'is_delete' => 0
        );
        $list = M( 'ScoreHot' )->where($map)->order('is_top desc')->group('score_id')->limit($limit)->select();

        $scoreIds = array_column($list,'score_id');
        $service = new ScoreService();
        $list = $service->getScorePageByScoreIds($scoreIds, $param);
        if($list) {
            $this->success( $list,200);
        } else {
            $this->fail('500');
        }
    }

    /**
     * 分页获取最新专区谱单
     */
    public function getScoreNew($param)
    {
        $limit = $param['limit'];
        $map = array(
            'is_delete' => 0
        );
        $list = M( 'ScoreHot' )->where($map)->order('is_top desc')->group('score_id')->limit($limit)->select();

        $scoreIds = array_column($list,'score_id');
        $service = new ScoreService();
        $list = $service->getScorePageByScoreIds($scoreIds, $param);

        if($list) {
            $this->success( $list,200);
        } else {
            $this->fail('500');
        }
    }

    /**
     * 通过谱单id获取谱单详情
     * @param $scoreId 谱单id
     * @return mixed
     */
    public function getScoreDetailByScoreId($param)
    {
        $uid = $param['uid'];
        $scoreId = $param['score_id'];
        if (empty($scoreId)) {
            $this->fail('600');
        }
        $list = M('Score')->where(array('id'=>$scoreId))->field('id,name,creator,create_time,is_private, author_type, comment_count, fav_count')->select();

        //获取谱单创建者信息及标签
        $service = new ScoreService();
        $list = $service->convertList($list);
        $scoreDetail = $list[0];

        //获取谱单封面
        $imageService = new ImageService();
        $image = $imageService->getImageUrlByScoreIds($scoreId);
        $scoreDetail['image'] = $image[$scoreId];


        //判断是否已收藏
        $map = array(
            'user_id' => $uid,
            'score_id' => $scoreId,
            'is_delete' => 0
        );
        $isFav = M('ScoreFavorite')->where($map)->select();
        if($isFav){
            $scoreDetail['is_favorite'] = 1;
        } else {
            $scoreDetail['is_favorite'] = 0;
        }
        //获取谱单内曲谱信息
        $sheetService = new SheetService();
        $scoreDetail['sheetlist'] = $sheetService->getSheetListByScoreId($scoreId, $param);

        //记录点击数
        M('score')->where(array('id'=>$scoreId))->setInc('click_count');

        $this->success($scoreDetail,200);
    }

    /**
     * 搜索谱单
     */
    public function getScoreBySearchName($param)
    {
        $keywords = explode(' ', $param['name']);
        foreach ($keywords as $key => $keyword) {
            $keywordArr[] = array('like','%'.$keyword.'%');
        }

        $map = array(
            'name' => $keywordArr,
            'is_private' => 0,
            'is_delete' => 0
        );
        $service = new ScoreService();
        $result = $service->getScoreListByMap($map, $param);

        $this->success($result,200);
    }

    /**
     * 添加评论
     */
    public function addComment($param)
    {
        if (empty($param['uid']) || empty($param['score_id']) || empty($param['remark'])) {
            $this->fail(600);
        }
        $service = new ScoreCommentService();
        try {
            $param['remark'] = htmlspecialchars($param['remark']);
            $res = $service->addComment($param);
            $this->success('', 200, '操作成功');
        } catch (\Exception $e) {
            $this->fail(500,'', $e->getMessage());
        }
    }

    /**
     * 获取评论
     */
    public function getComment($param)
    {
        if (empty($param['score_id'])) {
            $this->fail(600);
        }
        $service = new ScoreCommentService();
        $comment = $service->getComment($param);
        if ($comment) {
            $this->success($comment, 200);
        } else {
            $this->fail(500);
        }
    }

    /**
     * 删除评论
     */
    public function delComment($param)
    {
        if (empty($param['uid']) || empty($param['score_id']) || empty($param['comment_id'])) {
            $this->fail(600);
        }
        $creator = M('Score')->where(array('id'=>$param['score_id']))->getField('creator');
        if ($param['uid'] !== $creator) {
            $this->fail(403);
        }
        $service = new ScoreCommentService();

        $res = $service->delComment($param);
        if ($res) {
            $this->success('',200);
        } else {
            $this->fail(500);
        }

    }
}